package com.ydsungan.service.impl;

import com.ydsungan.common.DateUtils;
import com.ydsungan.dao.PracticeMapper;
import com.ydsungan.dao.QuestionMapper;
import com.ydsungan.dao.ReviewMapper;
import com.ydsungan.dao.UsedMapper;
import com.ydsungan.pojo.*;
import com.ydsungan.service.HomeService;
import com.ydsungan.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private PracticeMapper practiceMapper;

    @Autowired
    private UsedMapper usedMapper;

    @Autowired
    private ReviewMapper reviewMapper;

    @Override
    public long countQuestionByChapterId(QuestionExample questionExample) {
        return questionMapper.countByExample(questionExample);
    }
    @Override
    public long countPracticeByChapterId(PracticeExample practiceExample) {
        return practiceMapper.countByExample(practiceExample);
    }

    @Override
    public List<Question> listQuestion(QuestionExample questionExample) {
        return questionMapper.selectByExample(questionExample);
    }

    @Override
    public List<Practice> listPractice(PracticeExample practiceExample) {
        return practiceMapper.selectByExample(practiceExample);
    }

    @Override
    public int insertRecord(Practice practice) {
        return practiceMapper.insert(practice);
    }

    @Override
    public int updateQuestion(Question question) {
        return questionMapper.updateByPrimaryKeySelective(question);
    }

    @Override
    public int updatePracticeSelective(Practice practice) {
        return practiceMapper.updateByPrimaryKeySelective(practice);
    }

    @Override
    public Question findQuestionById(int id) {
        return questionMapper.selectByPrimaryKey(id);
    }

    @Override
    public int insertUsingRecord(Used used) {
        return usedMapper.insert(used);
    }

    @Override
    public List<Integer> getReviewQuestionsList(String openId,  String bookId) throws ParseException {

        int days[] = {1,2,6,30};
        List<Integer> daysList = new ArrayList<>();
        daysList.add(1);
        daysList.add(2);
        daysList.add(6);
        daysList.add(30);
        //以上是固有的

        //如果-1昨天没有复习，那么相对今天就应该复习前2天，前3天，前7天，前31天
        //如果-2天没有复习，那么相对今天就应该复习前3、4、8、32天，
        //如果-3。。。。。。。。。。。。。。。。。。4、5、9、33
        //-4，                                5、6、10、34
        //-5                                 6、7、11、35
        //-6                                 7、8、12、36
        //-7                                 8、9、13、37
        //如果昨天有复习，但是没有复习完呢？？
        for(int i = 1; i <= 7; ++i){
            List<Date> timeList = DateUtils.getDateStartAndEnd(i);
            ReviewExample reviewExample = new ReviewExample();
            ReviewExample.Criteria criteria = reviewExample.createCriteria();
            criteria.andUserIdEqualTo(openId);
            criteria.andBookIdEqualTo(Integer.parseInt(bookId));
            criteria.andCreateTimeBetween(timeList.get(0), timeList.get(1));
            List<Review> reviewList = reviewMapper.selectByExample(reviewExample);
            if(reviewList.size() == 0){
                //说明这一天没有复习
                //把 i + 1， i + 2，i + 6，i + 30，加入daysList
                for(int j = 0; j < days.length; ++j){
                    if(!daysList.contains(i + days[j])){
                        daysList.add(i + days[j]);
                    }
                }

            }
        }


        List<Integer> reviewQuestionsList = new ArrayList<>();

        for(int i = 0; i < daysList.size(); ++i){
            List<Date> timeList = DateUtils.getDateStartAndEnd(daysList.get(i));
            PracticeExample practiceExample = new PracticeExample();
            PracticeExample.Criteria criteria = practiceExample.createCriteria();
            criteria.andUserIdEqualTo(openId);
            criteria.andIsRightEqualTo(0);
            //criteria.andStatusEqualTo(1);
            criteria.andBookIdEqualTo(Integer.parseInt(bookId));
            criteria.andCreateTimeBetween(timeList.get(0), timeList.get(1));
            List<Practice> practiceList = practiceMapper.selectByExample(practiceExample);
            for(int j = 0; j < practiceList.size(); ++j){
                reviewQuestionsList.add(practiceList.get(j).getQuestionId());
            }
        }
        return reviewQuestionsList;
    }

    @Override
    public int insertReviewRecord(Review review) {
        return reviewMapper.insert(review);
    }
}
